<?php
/*后台登陆*/
namespace backend\controllers;
use yii;
use backend\models\Admin;
use yii\web\Session;

header('content-type:text/html;charset=utf-8');
class LoginController extends \yii\web\Controller
{
	public $enableCsrfValidation = false;
	//登陆
    public function actionIndex()
    {
    	if(yii::$app->request->isPost)
    	{
            //print_r($_POST);die;
    		$u_name=yii::$app->request->post('u_name');
    		$u_pass=yii::$app->request->post('u_pass');
    		$models = new Admin;
    		$results = $models->find()->where(['u_name'=>"$u_name",'u_pass'=>"$u_pass"])->one();
    		if ($results) {
                $u_id = $results['u_id'];
                $now = date('Y-m-d h:i:s',time());
                $db=Yii::$app->db;
                $sql="update admin set last_go_time = '$now' where u_id = $u_id";
                $sql2="update admin set go_num = go_num+1 where u_id = $u_id";
                $re=$db->createCommand($sql)->execute();
                $re2=$db->createCommand($sql2)->execute();
                $re3=$db->createCommand("select go_num,last_go_time from admin where u_id = $u_id")->queryOne();
                $time = $re3['last_go_time'];
                $num = $re3['go_num'];
                //用户信息存入session
                $session = Yii::$app->session;
                $session->set('u_id',$u_id);
                $session->set('u_name',$u_name);
                $session->set('go_time',$time);
                $session->set('go_num',$num);
                echo "<script>location.href='index.php?r=index/index'</script>";
    		}else{
    			echo "<script>alert('用户信息错误请重新登陆');window.history.go(-1);</script>";
    		}
    		exit;
    	}
        return $this->renderPartial('login');
    }
    //退出登录
    public function actionLoginout()
    {
        //清除session
        $session = Yii::$app->session;
        unset($session['u_id']);
        unset($session['u_name']);
        unset($session['go_time']);
        unset($session['go_num']);
        $this->redirect("index.php?r=login");
    }

}
